package com.back.mapper;

import com.back.entity.Movie;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface MovieDao {

    // 通过id查询电影
    @Select("select * from movie where id = ${movie_id}")
    Movie selectMovieById(@Param("movie_id")int movie_id);

    // 通过电影名模糊和类型列表查询电影，带分页
    @Select("<script>" +
            "select * from movie where name like '%${name}%' " +
            "<if test='types != null and types.size() > 0'>" +
            "and type in " +
            "<foreach item='type' collection='types' open='(' separator=',' close=')'>" +
            "#{type}" +
            "</foreach>" +
            "</if> " +
            "limit ${offset}, ${pagesize}" +
            "</script>")
    List<Movie> selectMovieByNameAndTypes(@Param("name") String name, @Param("types")List<String> types, @Param("offset")int offset, @Param("pagesize")int pagesize);

    // 通过电影名模糊和类型列表查询电影数量
    @Select("<script>" +
            "select count(*) from movie where name like '%${name}%' " +
            "<if test='types != null and types.size() > 0'>" +
            "and type in " +
            "<foreach item='type' collection='types' open='(' separator=',' close=')'>" +
            "#{type}" +
            "</foreach>" +
            "</if>" +
            "</script>")
    int selectMovieCountByNameAndTypes(@Param("name") String name, @Param("types")List<String> types);

    // 随机查询电影，限制数量
    @Select("select * from movie order by rand() limit ${limit}")
    List<Movie> selectRandomMovies(@Param("limit")int limit);

    // 添加电影
    @Insert("insert into movie(name, type, description, nation, date, img, page_view, total_score, num_score) " +
            "values(#{name}, #{type}, #{description}, #{nation}, #{date}, #{img}, #{page_view}, #{total_score}, #{num_score})")
    void insertMovie(Movie movie);

    // 修改电影
    @Update("update movie set name=#{name}, type=#{type}, description=#{description}, nation=#{nation}, date=#{date}, img=#{img}, page_view=#{page_view}, total_score=#{total_score}, num_score=#{num_score} where id=#{id}")
    void updateMovie(Movie movie);
}
